import sys
from PyQt4 import QtGui, QtCore
from box import *
from window import *
from enum_classes import *
import DSLParser
from questionMark_solver import *
import calendar
import calendar_utils
import calendar_file_utils

SELECTED = "BBBBEE"
#NOT_SELECTED = "EAFFA0"
WINTER = "CAE1FF"
SUMMER = "FFEC8B"
AUTUMN = "BC8F8F"
SPRING = "EAFFA0"


main_dict = None

sc = calendar_file_utils.SimpleCalendar("default.cal")

def select(row,col):
   calendar_utils.selected_day = calendar_utils.get_box_number(row,col)
   color_var_name = "color["+str(row)+"]["+str(col)+"]"
   repaint()
   main_dict["main_window"].change_var(color_var_name,SELECTED)
   new_text = sc.get(calendar_utils.selected_year,calendar_utils.selected_month,calendar_utils.selected_day)
   main_dict["main_window"].change_var("text_val",new_text)
   
def getColorByMonth(n):
   if ( (n==12) | (n==1) | (n==2) ):
      return WINTER
   if ( (n==3) | (n==4) | (n==5) ):
      return SPRING
   if ( (n==6) | (n==7) | (n==8) ):
      return SUMMER
   if ( (n==9) | (n==10) | (n==11) ):
      return AUTUMN

def getBackGroundByMonth(n):
   if ( (n==12) | (n==1) | (n==2) ):
      return "pictures/winter.png"
   if ( (n==3) | (n==4) | (n==5) ):
      return "pictures/spring.png"
   if ( (n==6) | (n==7) | (n==8) ):
      return "pictures/summer.png"
   if ( (n==9) | (n==10) | (n==11) ):
      return "pictures/autumn.png"

def setDates():
   ret = {}
   ret["header_text"]=calendar_utils.get_this_month_string()
   for i in range(6):
      for j in range(7) :
         nextLabel = calendar_utils.get_box_label(i*7 + j)
         ret["day_dates["+ str(i) + "][" + str(j) +"]" ] = nextLabel
         ret["color["+str(i)+"]["+str(j)+"]"] = getColorByMonth(calendar_utils.selected_month)
         if (nextLabel == ""):
            ret["set_mask["+ str(i) + "][" + str(j) +"]" ] = "pictures/hide.png"
         else:
            ret["set_mask["+ str(i) + "][" + str(j) +"]" ] = "pictures/show.png"
         
   day_row,day_col = calendar_utils.box_label_inverse(calendar_utils.selected_day)
   first_day = (calendar.weekday(calendar_utils.selected_year,calendar_utils.selected_month,1)+1)%7
   ret["color["+str(day_row)+"]["+str(day_col)+"]"] = SELECTED   
   return ret

def save_current_day():
   current_text = main_dict["main_window"].get_Gui_Var("text_val")
   sc.set(calendar_utils.selected_year,calendar_utils.selected_month,calendar_utils.selected_day, current_text)

def save_data(caller,event_name = None):
   save_current_day()
   sc.save()

def repaint():
   header_text = calendar_utils.get_this_month_string()
   main_dict["main_window"].change_var("header_text",header_text)
   for i in range(6):
      for j in range(7):
         nextLabel = calendar_utils.get_box_label(i*7 + j)
         main_dict["main_window"].change_var("color["+str(i)+"]["+str(j)+"]",getColorByMonth(calendar_utils.selected_month))
         main_dict["main_window"].change_var("day_dates["+ str(i) + "][" + str(j) +"]",nextLabel)
         if (nextLabel == ""):
             main_dict["main_window"].change_var("set_mask["+ str(i) + "][" + str(j) +"]" , "pictures/hide.png")
         else:
             main_dict["main_window"].change_var("set_mask["+ str(i) + "][" + str(j) +"]" , "pictures/show.png")
   main_dict["main_window"].change_var("prev_color",getColorByMonth(calendar_utils.selected_month))
   main_dict["main_window"].change_var("next_color",getColorByMonth(calendar_utils.selected_month))
   main_dict["main_window"].change_var("save_color",getColorByMonth(calendar_utils.selected_month))
            
def prev_month(caller,event_name = None):
   save_current_day()
   calendar_utils.go_prev_month()
   select(0,(calendar.weekday(calendar_utils.selected_year,calendar_utils.selected_month,1)+1)%7)
   main_dict["main_window"].change_var("image_var",getBackGroundByMonth(calendar_utils.selected_month))

def next_month(caller,event_name = None):
   save_current_day()
   calendar_utils.go_next_month()
   select(0,(calendar.weekday(calendar_utils.selected_year,calendar_utils.selected_month,1)+1)%7)
   main_dict["main_window"].change_var("image_var",getBackGroundByMonth(calendar_utils.selected_month))
   
def day_clicked(self, event_name):
   row = int(event_name[12])
   col = int(event_name[15])
   save_current_day()
   select(row,col)

def setInitialButtonColor():
   ret = {}
   color = getColorByMonth(calendar_utils.selected_month)
   ret["prev_color"] = color
   ret["next_color"] = color
   ret["save_color"] = color
   return ret

def splittermoved(self, event_name):
   new_size = main_dict["main_window"].get_Gui_Var("splittersize")[0]
   main_dict["main_window"].change_var("day_buttons_height",new_size)

day_list =["SUN","MON","TUE","WED","THU","FRI","SAT"] 

def updatebuttons(self,prev,new):
   new_button_size = (new-main_dict["main_window"].get_Gui_Var("label_height[SUN]"))/6  -2
   for i in range(6):
      main_dict["main_window"].change_var("row_height[%d]" % i, new_button_size)
      main_dict["main_window"].change_var("row_y[%d]" % i, i*new_button_size)
      for j in range(7):
         main_dict["main_window"].change_var("button_height[%d][%d]" % (i,j),new_button_size)

if __name__ == '__main__':
   if len(sys.argv)>1:
      filename = sys.argv[1]
   else:
      filename = "calendar_dsl.txt"
   app = QtGui.QApplication(sys.argv)
   parsed_file = DSLParser.parseFile(filename)
   box_dict = parsed_file[0]
   main_dict = solve(box_dict)
   main_dict["main_window"].bind("save_clicked",save_data)
   main_dict["main_window"].bind("prev_clicked",prev_month)
   main_dict["main_window"].bind("next_clicked",next_month)
   for i in range(6):
      for j in range(7):
         main_dict["main_window"].bind("day_clicked[%d][%d]" % (i,j), day_clicked)


   if main_dict["main_window"].event_dict.has_key("splittermoved"):
      main_dict["main_window"].bind("splittermoved",splittermoved)
      main_dict["main_window"].when_changed("day_buttons_height",updatebuttons)

   main_d = setDates()
   main_d.update(setInitialButtonColor())
   main_d["text_val"] = "Hi!\n\nWelcome to the DSL calendar by:\n\n*\tYaacov Dar\n*\tMichael Be\'eri\n*\tAmit Liber"
   main_d["image_var"] = getBackGroundByMonth(calendar_utils.selected_month)
   main_dict["main_window"](main_d) 
   sys.exit(app.exec_())
